<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/vue"></script>
    <script type="text/x-template" id="mycomp">
        <span>
            <div>总共{{total}}个事项，已完成{{done}}个事项，还有{{total-done}}个事项未完成。</div>
        </span>
    </script>
    <script>
        window.onload=function(){

            Vue.component("todoWord",{
                props:['total','done'],
                template:"#mycomp"

            })

            var app4 = new Vue({
                el:'#app-4',

                methods:{
                    add:function(){
                        this.todos.push({name:this.thing,done:false});
                    },
                    switchdel:function(idx){
                        this.todos[idx].done=!this.todos[idx].done;
                    },

                },
                computed:{
                    done:function(){
                        var count=0;
                        for(var i=0;i<this.todos.length;i++){
                            if(this.todos[i].done){
                                count++;
                            }
                        }
                        return count;
                    }
                },
                data:{
                    message:'Todo List',
                    thing:null,
                    todos:[
                        {name:'吃饭',done:false},
                        {name:'睡觉',done:false},
                        {name:'哈哈哈',done:true}
                    ]
                }
            })
        }
    </script>
    <style>
        .del{text-decoration: line-through}
    </style>
</head>
<body>
<div id="app-4">
    <h1>{{message}}</h1>
    <input type="text" v-model="thing" v-on:keyup.enter="add"/>
    <button v-on:click="add">添加</button>

    <div v-for="(todo,idx) in todos">
        <input type="checkbox" v-bind:checked="todo.done" v-on:click="switchdel(idx)">{{idx}}.
        <span v-bind:class="{del:todo.done}">{{todo.name}}</span>

    </div>
    <todo-word :total="todos.length" :done="done"></todo-word>
</div>
</body>
</html>